﻿using GoodShop.seckill;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace GoodShop.ShopCar
{
    public class ShopCarService : CrudAppService<ShoppingCar, ShoppingCarDTO, Guid, PagedAndSortedResultRequestDto, CreateUpdateShoppingCarDTO>, IShopCarService
    {
        public ShopCarService(IRepository<ShoppingCar, Guid> repository) : base(repository)
        {
        }
        /*   [HttpPost, Route("ddddddd")]
         public int add(ShoppingCarDTO shoppingCarDTO)
          {
              RedisHelper redisHelper = new RedisHelper();
              redisHelper.Set("r12", shoppingCarDTO);
              return 1;
          }*/
        //加入购物车Redis



        [HttpPost, Route("abp/app/shopingcarAdd")]
        public int AddRedis(RedisDTO redisDto)
        {
            //商品数量默认为1
            //redisDto.ShoppingCarNum = 1;
            //实例化redis
            RedisHelper redis = new RedisHelper();
            //定义集合接收信息
            List<RedisDTO> ls1 = new List<RedisDTO>();
            //获取原信息
            var ls2 = redis.Get<List<RedisDTO>>("r123");
            if (ls2 != null)//不为空时获取购物车信息
            {
                ls1 = redis.Get<List<RedisDTO>>("r123");
            }
            //定义标记
            int flage = 0;
            if (ls1 != null)//不为空时循环
            {
                foreach (var item in ls1)
                {
                    if (item.Id == redisDto.Id)
                    {
                        item.ShoppingCarNum += redisDto.ShoppingCarNum;//增加数量
                        flage += 1;
                    }
                }
            }
            if (flage == 0)//没有重复信息
            {
                ls1.Add(redisDto);
            }
            // 添加
            redis.Set("r123", ls1);
            return 1;//返回信息
        }

        //定义标记
        /* int flage = 0;
         if (ls1 != null)//不为空时循环
         {
             foreach (var item in await ls1)
             {
                 if (item.Id == redisDto.Id)
                 {
                     item.ShoppingCarNum += redisDto.ShoppingCarNum;//增加数量
                     flage += 1;
                 }
             }
         }
         if (flage == 0)//没有重复信息
         {
             ls1.Add(redisDto);
         }
         //添加
         redis.Set("r12", ls1);
         return 1;//返回信息/*
     }
        */
        //显示购物车信息
        [HttpGet("abp/Carget")]
        public List<RedisDTO> ShowRedis()  //Redis 显示购物车
        {
            RedisHelper redis = new RedisHelper();
            List<RedisDTO> xx = new List<RedisDTO>();
            xx = redis.Get<List<RedisDTO>>("r123");
            // int sl = xx.Count();
            return xx;
        }
    }
}
